package vue;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

import modele.ModeleClasse;


public class VuePanelClasse extends JPanel 
{
	private ModeleClasse m;
	
	private static final long serialVersionUID = 1L;
	
	private JPanel nom;
	private JPanel attributs; 
	private JPanel methodes;
	private JLabel labelNom;
	private JLabel labelAttributs;
	private JLabel labelMethodes;
	private int nbAtt;
	private int nbMeth;
	
	public static Color couleurSelection = new Color(102,204,204);
	public static Color couleurNormal = new Color(255,204,102);
	
	public int xPrec;
	public int yPrec;
	

	public VuePanelClasse(ModeleClasse mm)
	{
		this.m = mm;
		
		this.nom = new JPanel();
		this.attributs = new JPanel();
		this.methodes = new JPanel();
		
		this.labelNom = new JLabel(mm.getNom());
		
		int largeurMax = 1;
		char[] c = mm.getNom().toCharArray();
		largeurMax = c.length;
		
		/* Affichage des attributs */
		this.labelAttributs = new JLabel();
		nbAtt = mm.getAttributs().size();
		labelAttributs.setText("<html>");
		for(int i = 0 ; i < nbAtt ; i ++) {
			labelAttributs.setText(labelAttributs.getText()+"+ "+mm.getAttributs().get(i).getNom()+"<br/>");
			
			c = mm.getAttributs().get(i).getNom().toCharArray();
			if(c.length > largeurMax)
				largeurMax = c.length;
		}
		labelAttributs.setText(labelAttributs.getText()+"</html>");
		
		/* Affichage des méthodes */
		this.labelMethodes = new JLabel();
		nbMeth = mm.getMethodes().size();
		labelMethodes.setText("<html>");
		for(int i = 0 ; i < nbMeth ; i ++) {
			labelMethodes.setText(labelMethodes.getText()+"+ "+mm.getMethodes().get(i).getNom()+"()<br/>");

			c = mm.getMethodes().get(i).getNom().toCharArray();
			if(c.length > largeurMax)
				largeurMax = c.length;
		}
		labelMethodes.setText(labelMethodes.getText()+"</html>");
		
		this.nom.add(labelNom);
		this.attributs.add(labelAttributs);
		this.methodes.add(labelMethodes);
		
		
		//this.setLayout((LayoutManager) new BoxLayout(this, BoxLayout.Y_AXIS));
		this.setLayout(new BorderLayout());
		
		int largeur = 20 * largeurMax;
		
		this.nom.setPreferredSize(new Dimension(largeur,25));
		this.nom.setBorder(BorderFactory.createLineBorder(Color.black));
		this.nom.setBackground(new Color(255,204,102));
		this.add(nom, BorderLayout.NORTH);
		
		this.attributs.setPreferredSize(new Dimension(largeur,10+(20*nbAtt)));
		this.attributs.setBorder(BorderFactory.createLineBorder(Color.black));
		this.attributs.setBackground(new Color(255,204,102));
		this.add(attributs, BorderLayout.CENTER);
		
		this.methodes.setPreferredSize(new Dimension(largeur,10+(20*nbMeth)));
		this.methodes.setBorder(BorderFactory.createLineBorder(Color.black));
		this.methodes.setBackground(new Color(255,204,102));
		this.add(methodes, BorderLayout.SOUTH);
		
		this.setPreferredSize(new Dimension(largeur,25+20+(nbAtt*20)+(nbMeth*20)));
		this.setBorder(BorderFactory.createLineBorder(Color.black));
		
		this.repaint();
	}

	public void paintComponent(Graphics g) {
		//this.getGraphics().drawString(m.getNom(), 10, 10);
		//System.out.println(m.getNom());
	}
	
	public void modifierNom(String n) {
		this.labelNom.setText(n);
		this.repaint();
	}

	public void colorier(Color c) {
		this.nom.setBackground(c);
		this.attributs.setBackground(c);
		this.methodes.setBackground(c);
	}
	
	public ModeleClasse getModele() {
		return this.m;
	}
}
